/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package searchreplace;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author TQNINH
 */
public class WordReplacer {
    
    
    public WordReplacer() {
        
    }
    
    public static void main(String[] args) {
        try {
            FileReader reader = new FileReader("Input.txt");
            BufferedReader bufReader = new BufferedReader(reader);
            String line;

            FileWriter writer = new FileWriter("Output.txt", true);
            BufferedWriter bufWriter = new BufferedWriter(writer);
            int wordCount = 0;

            while((line = bufReader.readLine()) != null) {
                String regex = "\\sIs\\s";
                Pattern pat = Pattern.compile(regex);
                Matcher match = pat.matcher(line);
                while(match.find()) {
                    wordCount++;
                }

                String str = match.replaceAll(" Was ");

                regex = "\\sis\\s";
                pat = Pattern.compile(regex);
                match = pat.matcher(str);

                while(match.find()) {
                    wordCount++;
                }

                String lower = match.replaceAll(" was ");

                regex = "^Is\\s";
                pat = Pattern.compile(regex);
                match = pat.matcher(lower);

                while(match.find()) {
                    wordCount++;
                }

                String output = match.replaceAll("Was ");

                bufWriter.write(output);
                bufWriter.write("\n");
            }
            System.out.println("Number of instance of 'is' found in the "
                    + "input file = " + wordCount);
            bufWriter.flush();

            FileReader checker = new FileReader("Output.txt");
            bufReader = new BufferedReader(checker);
            int wordReplaced = 0;
            while((line = bufReader.readLine()) != null) {
                Pattern pattern = Pattern.compile("Was\\s*");
                Matcher match = pattern.matcher(line);
                while(match.find()) {
                    wordReplaced++;
                    System.out.printf("Found: \"%s\" from %d to %d.%n\n",
                            match.group(), match.start(), match.end());
                }

                pattern = Pattern.compile("\\swas\\s*");
                match = pattern.matcher(line);
                while(match.find()) {
                    wordReplaced++;
                    System.out.printf("Found: \"%s\" from %d to %d.%n\n",
                            match.group(), match.start(), match.end());
                }
            }
            System.out.println("Number of instance of ' was ' found in the "
                    + "output file = " + wordReplaced);

            bufWriter.close();
            bufReader.close();
            reader.close();
            writer.close();
            checker.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
